package org.sakai.controller;

import org.sakai.dto.UserDTO;
import org.sakai.entity.ApiResponseEntity;
import org.sakai.entity.ErrorCode;
import org.sakai.entity.Result;
import org.sakai.entity.User;
import org.sakai.entity.vo.UserVO;
import org.sakai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody UserDTO loginDTO) {
        String email = loginDTO.getEmail();
        String password = loginDTO.getPassword();
        String phone = loginDTO.getPhone();
        String verificationCode = loginDTO.getVerifyCode();
        //默认结果是未知错误
        Result<String> result = Result.error(ErrorCode.UNKNOWN_ERROR);
        //通过邮箱注册
        if( email != null){
            try {
                result = userService.createUserByEmail(email, password, verificationCode);
            }catch (Exception e){
                e.printStackTrace();
                return ApiResponseEntity.error(ErrorCode.UNKNOWN_ERROR);
            }
        }
        //通过电话注册
        else if( phone != null){
            result = userService.createUserByPhone(phone, password, verificationCode);
        }
        //检查是否成功
        return ApiResponseEntity.getResponseEntity(result);
    }

    /**
     * 通过各种方式登录
     * @param loginDTO
     * @return 若成功登录会返回登录token字符串
     */
    @PostMapping("/login")
    public ResponseEntity<UserVO> login(@RequestBody UserDTO loginDTO){
        String email = loginDTO.getEmail();
        String password = loginDTO.getPassword();
        String phone = loginDTO.getPhone();
        String verificationCode = loginDTO.getVerifyCode();
        //默认结果是未知错误
        Result<UserVO> result = Result.error(ErrorCode.UNKNOWN_ERROR);
        if(password != null){
            if(email != null){
                //通过邮箱和密码登录
                result = userService.loginByEmailAndPassword(email, password);
            } else if (phone != null) {
                //通过手机和密码登录
//                result = userService.loginByPhoneAndPassword(phone, password);
            }
        }
        if(verificationCode != null){
            if(email != null){
                //通过邮箱和验证码登录
                result = userService.loginByEmailAndVerifyCode(email, verificationCode);
            }else if (phone != null) {
                //通过手机和验证码登录
//                result = userService.loginByPhoneAndVerifyCode(phone, verificationCode);
            }
        }
        //检查是否成功
        return  ApiResponseEntity.getResponseEntity(result);
    }

    /**
     * 发送邮箱验证码
     * restful格式api
     * @param email
     * @return
     */
    @GetMapping("/sendEmailCode/{email}")
    public ResponseEntity<String> sendEmailCode(@PathVariable String email){
        //默认结果是未知错误
        Result<String> result = Result.error(ErrorCode.UNKNOWN_ERROR);
        if(email != null){
            result = userService.sendEmailCode(email);
        }
        //检查是否成功
        return ApiResponseEntity.getResponseEntity(result);
    }

    @GetMapping("/info/{id}")
    public ResponseEntity<UserVO> getUserInfo(@PathVariable String id){
        Result<UserVO> result = userService.getUserInfo(id);
        return ApiResponseEntity.getResponseEntity(result);
    }
}
